// working for cityLogic

// 引入数据库
const db = require('../utils/db');

async function getResourcesInfo(userid) {
    const sql = `select * from t_resources where userid = ?`;
    const result = await db.query(sql, userid);
    return result;
}

// 获取type 类型：player stones hood food iron blank sliver
getType = (n) => {
    let chars = ['player', 'food', 'stone', 'hood', 'iron', 'sliver', 'blank', 'blank', 'blank', 'player']; // 3空地2玩家
    let res = '';
    for (let i = 0; i < n; i++) {
        let id = Math.ceil(Math.random() * chars.length - 1);
        res += chars[id];
    }
    return res;
}

// 获取随机name
generateRandomChineseName = () => {
    const familyNames = [
        "王", "李", "张", "刘", "陈", "杨", "赵", "黄", "周", "吴",
        "徐", "孙", "胡", "朱", "高", "林", "何", "郭", "马", "罗"
    ];
    const givenNames = [
        "伟", "磊", "洋", "勇", "军", "杰", "涛", "明", "刚", "强", "芳", "娜", "敏", "静", "丽", "娟", "霞", "秀英", "华", "平"
    ];
    let familyNum = parseInt(Math.random() * familyNames.length);
    let givenNum = parseInt(Math.random() * givenNames.length);
    return familyNames[familyNum] + givenNames[givenNum];
}

// 获取等级 1- 100
getLevel = (n, m) => {
    return Math.floor(Math.random() * (m - n + 1)) + n;
}

// 设置xy
setXY = (x, y, blockNum) => {
    if (y < blockNum) {
        y += 1;
    } else if (y == blockNum) {
        if (x < blockNum) {
            x += 1;
            y = 1;
        }
    }
    return { x, y };
}

// 初始化世界模块表 有43万块，系统自动加入
async function initWorldModule() {
    let type = null;
    let name = null;
    let lv = 1;
    let x = 1;
    let y = 0;
    let blockNum = 660; // 660*660=435600

    actTask(type, name, lv, x, y, blockNum);
}

function actTask(type, name, lv, x, y, blockNum) {
    type = getType(1);
    if (type == 'player') {
        name = generateRandomChineseName(); // 生成随机中文名字
    } else {
        name = null;
    }
    lv = getLevel(1, 100);

    let res = setXY(x, y, blockNum);
    x = res.x;
    y = res.y;
    const sql = 'INSERT INTO t_block (type, name, lv, x, y) VALUES (?, ?, ?, ?, ?)'

    insertDate(sql, type, name, lv, x, y, (result) => {
        if (result) {
            if (x == blockNum && y == blockNum) {
                console.log('插入成功');
                return result;
            } else {
                return actTask(type, name, lv, x, y, blockNum);
            }
        } else {
            console.log('插入失败');
        }
    }) 
}

async function insertDate(sql, type, name, lv, x, y, callback) {
    let result = await db.query(sql, [type, name, lv, x, y]);
    if (result) {
        callback(result);
    } else {
        callback(null);
    }
}

// 第一次运行插入完数据库之后关掉
// initWorldModule();

// 获取周围的block数据
async function getBlockData(x, y) {
    x = parseInt(x);
    y = parseInt(y);
    let xMin = x - 10;
    let xMax = x + 10;
    let yMin = y - 10;
    let yMax = y + 10;
    const sql = 'SELECT * FROM t_block WHERE x >= ? AND x <= ? AND y >= ? AND y <= ?';
    const result = await db.query(sql, [xMin, xMax, yMin, yMax]);
    return result;
}

module.exports = {
    getResourcesInfo,    
    getBlockData,
}